<template>
    <!-- 总容器 -->
    <div class="wrapper">
        <header>
            <i class="fa fa-angle-left" @click="goBack"></i>
            <p>体检报告</p>
            <div></div>
        </header>

        <nav>
            <div class="active">总检结论</div>
            <div>报告详情</div>
        </nav>

        <div class="top-ban"></div>

        <div class="nav-content-item">
            <div class="item">
                <div class="title">异常项</div>
                <ul>
                    <li v-for="(item, index) in reportlist" :key="index">
                        <div
                            class="indications"
                            v-for="(detail, index) in item.cidetailedreports"
                            v-show="detail.iserror === 1"
                            :key="index"
                        >
                            <div class="left">
                                <div>异</div>
                                <p>{{ detail.name }}</p>
                            </div>
                            <div class="right">
                                <p>{{ detail.value }}</p>
                                <p>正常值范围：{{ detail.normalvaluestring }}</p>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
            <div class="item">
                <div class="title">一、尊敬的顾客，您本次体检结论如下：</div>
                <ul>
                    <li class="conclusion-title" v-for="(item, index) in resultlist" :key="index">
                        {{ index + 1 }}、{{ item.title }}
                    </li>
                </ul>
            </div>
            <div class="item">
                <div class="title">二、尊敬的顾客，您本次体检建议信息如下：</div>
                <ul>
                    <li class="conclusion-title" v-for="(item, index) in resultlist" :key="index">
                        {{ index + 1 }}、{{ item.title }}
                    </li>
                    <li class="conclusion-content" v-for="(item, index) in resultlist" :key="index">
                        <h3>{{ index + 1 }}、{{ item.title }}</h3>
                        <p>{{ item.content }}</p>
                    </li>
                </ul>
            </div>
        </div>

        <div class="nav-content-item">
            <div class="item" v-for="(item, index) in reportlist" :key="index">
                <div class="title">{{ item.ciname }}</div>
                <ul>
                    <li v-for="(detail, index) in item.cidetailedreports" :key="index">
                        <div class="indications">
                            <div class="left">
                                <div v-show="detail.iserror==1">异</div>
                                <p>{{ detail.name }}</p>
                            </div>
                            <div class="right">
                                <p>{{ detail.value }}</p>
                                <p>正常值范围：{{ detail.normalvaluestring }}</p>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
        </div>

        <div class="bottom-ban"></div>
        <tijianfooter></tijianfooter>
    </div>
</template>

<script setup>
import "../../style/css/report.css";
import tijianfooter from "../../components/tijianfooter.vue";
import { useRouter, useRoute } from "vue-router";
import { getSessionStorage } from "@/utils/common";
import { ref, onMounted, nextTick } from "vue";
import axios from "axios";

const route = useRoute();
const user = ref(getSessionStorage("user"));
const resultlist = ref([]);
const reportlist = ref([]);

const fetchOrderList = async () => {
    try {
        const res = await axios.post("/api/result/getListbyuser", {
            orderid: route.query.orderid,
        });
        resultlist.value = res.data.data || [];
        console.log(res, "result");
    } catch (error) {
        console.error("获取订单列表失败:", error.response?.data?.message || error.message);
    }
};

const fetchReportList = async () => {
    try {
        const res = await axios.post("/api/report/getReportList", {
            orderid: route.query.orderid,
        });
        reportlist.value = res.data.data || [];
        console.log(res, "report");
    } catch (error) {
        console.error("获取报告列表失败:", error.response?.data?.message || error.message);
    }
};

// 返回上一页
const goBack = () => {
    history.go(-1);
}

onMounted(async () => {
    await fetchOrderList();
    await fetchReportList();
    setupNavInteraction();
});

// 设置导航交互
const setupNavInteraction = () => {
    nextTick(() => {
        let navElement = document.getElementsByTagName("nav")[0];
        if (navElement) {
            let navItemArr = navElement.getElementsByTagName("div");
            let navContentItemArr = document.getElementsByClassName("nav-content-item");

            function work(index) {
                if (index < 0 || index >= navItemArr.length) {
                    console.error("索引超出范围:", index);
                    return;
                }
                
                // 重置初始状态
                Array.from(navItemArr).forEach((item, i) => {
                    item.style.borderBottom = "none";
                    item.style.color = "#555";
                    if (navContentItemArr[i]) {
                        navContentItemArr[i].style.display = "none";
                    }
                });

                // 设置当前选中状态
                navItemArr[index].style.borderBottom = "solid 2px #137E92";
                navItemArr[index].style.color = "#137E92";
                if (navContentItemArr[index]) {
                    navContentItemArr[index].style.display = "block";
                }
            }

            // 设置初始状态
            work(0);

            // 为每个导航项添加事件监听
            Array.from(navItemArr).forEach((item, i) => {
                item.onclick = () => work(i);
            });
        } else {
            console.error("找不到<nav>元素");
        }
    });
};

</script>

<style scoped>
header {
    width: 100%;
    height: 15.7vw;
    background-color: #fff;

    position: fixed;
    left: 0;
    top: 0;

    display: flex;
    align-items: center;
    justify-content: space-between;

    box-sizing: border-box;
    padding: 0 3.6vw;
}
header .fa {
    font-size: 8vw;
}

/*********************** footer ***********************/
footer {
    width: 100%;
    height: 14.2vw;
    box-sizing: border-box;
    border-top: solid 1px #e9e9e9;
    background-color: #fff;

    position: fixed;
    left: 0;
    bottom: 0;
}
footer ul {
    width: 100%;
    height: 14.2vw;
    display: flex;
    align-items: center;
    justify-content: space-around;
}
footer ul li {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    font-size: 3vw;
    color: #999;

    user-select: none;
    cursor: pointer;
}
footer ul li .fa {
    font-size: 5vw;
}

/*********************** common样式 ***********************/
.top-ban {
    width: 100%;
    height: 31.7vw;
}
.bottom-ban {
    width: 100%;
    height: 14.2vw;
}

/*********************** nav ***********************/
nav {
    width: 100%;
    height: 16vw;
    display: flex;
    background-color: #f9f9f9;

    position: fixed;
    left: 0;
    top: 15.7vw;
}
nav div {
    flex: 1;
    height: 14vw;
    box-sizing: border-box;

    text-align: center;
    line-height: 14vw;
    font-size: 4.2vw;
    font-weight: 600;
    color: #555;
}

.active {
    border-bottom: solid 2px #137e92;
    color: #137e92;
}

/*********************** nav-content-item ***********************/
.nav-content-item .item {
    width: 92vw;
    margin: 0 auto;
    margin-top: 3vw;
    margin-bottom: 3vw;
    overflow: hidden;
    border-radius: 3vw;
    background-color: #fff;
}
.nav-content-item .item .title {
    width: 100%;
    height: 10vw;
    background-color: #7bafd7;
    line-height: 10vw;
    box-sizing: border-box;
    padding-left: 4vw;
    font-size: 4.2vw;
    color: #fff;
}

.nav-content-item .item ul {
    width: 100%;
}

/****** 数值型检查项样式 ******/
.nav-content-item .item ul li {
    border-bottom: solid 1px #eee;
}
.nav-content-item .item ul li:last-child {
    border-bottom: none;
}
.nav-content-item .item ul li .indications {
    width: 100%;
    height: 14vw;
    padding: 0 3vw;
    display: flex;
    align-items: center;
    font-size: 3.2vw;
    color: #333;
}

.nav-content-item .item ul li .indications .left {
    flex: 1;
    display: flex;
}
.nav-content-item .item ul li .indications .left div {
    background-color: #ba634e;
    color: #fff;
    padding: 0.2vw 0.6vw;
    border-radius: 0.6vw;
}
.nav-content-item .item ul li .indications .left p {
    font-weight: 600;
    margin-left: 2vw;
}
.nav-content-item .item ul li .indications .right {
    flex: 1;
}
.nav-content-item .item ul li .indications .right p:last-child {
    color: #999;
}
/****** 数值型检查项样式 ******/

/****** 描述型检查项样式 ******/
.nav-content-item .item ul li .indications-type-4 {
    width: 100%;
    box-sizing: border-box;
    padding: 0 3vw;

    font-size: 3.2vw;
    color: #333;
}
.nav-content-item .item ul li .indications-type-4 div {
    margin: 5vw 0;
}
.nav-content-item .item ul li .indications-type-4 div:first-child {
    font-weight: 600;
}
.nav-content-item .item ul li .indications-type-4 div:last-child {
    color: #999;
}
/****** 描述型检查项样式 ******/

.nav-content-item .item ul .conclusion-title {
    width: 100%;
    height: 12vw;
    box-sizing: border-box;
    border-bottom: solid 1px #eee;
    padding: 0 3vw;

    display: flex;
    align-items: center;
    font-size: 4vw;
    color: #555;
}
.nav-content-item .item ul .conclusion-title:last-child {
    border-bottom: none;
}

.nav-content-item .item ul .conclusion-content {
    width: 100%;
    box-sizing: border-box;
    border-bottom: solid 1px #eee;
    padding: 4vw 3vw;
    font-size: 3.6vw;
    color: #555;
}
.nav-content-item .item ul .conclusion-content:last-child {
    border-bottom: none;
}
.nav-content-item .item ul .conclusion-content h3 {
    font-size: 4vw;
    font-weight: 600;
    margin-bottom: 2vw;
}
.nav-content-item .item ul .conclusion-content p {
    text-indent: 8vw;
}
</style>
